#include <wm/window.h>
#include <nxos/syscall.h>

#define W 800
#define H 600

// #define USE_FULLSCREEN_DESKTOP

int main(int argc, char **argv)
{
    NX_WindowInit();

#ifndef USE_FULLSCREEN_DESKTOP    
    NX_Window *win = NX_WindowCreate(0, 0, W, H, NX_ColorRGB(0xffffff), 0);
    NX_WindowShow(win);
    NX_WindowUpdate(win);

    NX_ThreadSleep(500);

    NX_WindowResize(win, W /2, H / 2);

    NX_ThreadSleep(500);

    NX_WindowResize(win, W, H);
#else
    NX_Window *win = NX_WindowCreate(0, 0, W, H, NX_ColorRGB(0xffffff), NX_WINDOW_FLAG_FULLSCREEN_DESKTOP);
    NX_WindowShow(win);
    NX_WindowUpdate(win);
#endif

    NX_Surface * s = win->connect.surface;
    int i = 0;

    NX_TimeVal tv = NX_ClockGetMillisecond();
    int fps = 0;

    while (1)
    {
        NX_WindowEvent e;
        if (NX_WindowPollEvent(win, &e) != NX_EOK)
        {
            NX_SurfaceClear(s, NX_ColorRGB(i),
                0, 0, s->width, s->height);
            i+=10;
            NX_WindowUpdate(win);

            fps++;
            if (NX_ClockGetMillisecond() - tv > 1000)
            {
                NX_Printf("fps:%d\n", fps);
                tv = NX_ClockGetMillisecond();
                fps = 0;
            }
        }
        else
        {
            switch (e.type)
            {
            case NX_ABS_EVENT:
                NX_Printf("[msg/NX_ABS_EVENT] x=%d,y=%d,x_root=%d,y_root=%d\n",
                    e.abs_event.x, e.abs_event.y, e.abs_event.x_root, e.abs_event.y_root);
                break;
            case NX_KEYDOWN_EVENT:
                NX_Printf("[msg/NX_KEYDOWN_EVENT] key=%d, mod=%x\n", e.keydown_event.code, e.keydown_event.modify);
                if (e.keydown_event.code == NX_KEY_Q || e.keydown_event.code == NX_KEY_q)
                {
                    goto out;
                }
                break;
            case NX_KEYUP_EVENT:
                NX_Printf("[msg/NX_KEYUP_EVENT] key=%d, mod=%x\n", e.keyup_event.code, e.keyup_event.modify);
                break;
            case NX_MOUSEBUTTONDOWN_EVENT:
                NX_Printf("[msg/NX_MOUSEBUTTONDOWN_EVENT] x=%d,y=%d,code=%d\n",
                    e.mouse_buttondown_event.x, e.mouse_buttondown_event.y, e.mouse_buttondown_event.code);
                break;
            case NX_MOUSEBUTTONUP_EVENT:
                NX_Printf("[msg/NX_MOUSEBUTTONUP_EVENT] x=%d,y=%d,code=%d\n",
                    e.mouse_buttonup_event.x, e.mouse_buttonup_event.y, e.mouse_buttonup_event.code);
                break;
            case NX_MOUSEWHEELUP_EVENT:
                NX_Printf("[msg/NX_MOUSEWHEELUP_EVENT] x=%d,y=%d\n",
                    e.mouse_wheel_event.x, e.mouse_wheel_event.y);
                break;
            case NX_MOUSEWHEELDOWN_EVENT:
                NX_Printf("[msg/NX_MOUSEWHEELDOWN_EVENT] x=%d,y=%d\n",
                    e.mouse_wheel_event.x, e.mouse_wheel_event.y);
                break;
            case NX_FLUSH_EVENT:
                NX_Printf("[msg/NX_FLUSH_EVENT]\n");
                break;
            case NX_MOUSE_IN_EVENT:
                NX_Printf("[msg/NX_MOUSE_IN_EVENT]\n");
                break;
            case NX_MOUSE_OUT_EVENT:
                NX_Printf("[msg/NX_MOUSE_OUT_EVENT]\n");
                break;
            default:
                break;
            }
        }
    }
out:
    NX_WindowDestroy(win);
    NX_WindowExit();
    return 0;
}